package com.ytd.common.util;

import com.ytd.common.bean.LocalServerSetting;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 读取文件工具类
 *
 * @author gaoguanchao
 * @since 2024/1/15 0015
 */
public class ReadFileUtil {
    public static final String LOCAL_SERVER_SETTING = "ServerSetting.txt";
    public static final String Download_Directory = "/storage/emulated/0/Download/";

    public static LocalServerSetting readServerSetting() {
        String serverSettingString = readFile(Download_Directory + LOCAL_SERVER_SETTING);
        return GsonUtil.fromJson(serverSettingString, LocalServerSetting.class);
    }

    public static void delServerSettingFile() {
        File file = new File(Download_Directory + LOCAL_SERVER_SETTING);
        if (file.exists()) {
            boolean delete = file.delete();
            if (delete) {
                LogUtil.i("delServerSettingFile");
            }
        }
    }

    public static String readFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                return str;
            }
            in.close();
        } catch (IOException e) {
        }
        return null;
    }
}
